﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using RMAS.DAL;
using Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmDM_GroupMeterial : Form
    {
        EnumGUI.ThaoTac thaotac;
        DM_METERIALGROUP_Provider prov = null;
        DM_METERIALGROUP Currobj = null;
        private SqlConnection con;

        public frmDM_GroupMeterial(DM_METERIALGROUP _obj, EnumGUI.ThaoTac _thaotac)
        {
            InitializeComponent();
            this.thaotac = _thaotac;
            this.Currobj = _obj;
        }
        private void frmDM_GroupMeterial_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        private void LoadAll()
        {
            con = (SqlConnection)StaticVar.ServerConnection;
            prov = new DM_METERIALGROUP_Provider();
            ControlUI(thaotac);
        }

        private void ControlUI(EnumGUI.ThaoTac thaotac)
        {
            if (thaotac == EnumGUI.ThaoTac.them || thaotac == EnumGUI.ThaoTac.sua)
            {
                SetStateButton(thaotac);
                SetTextReadOnly(false);
                loadToUI(Currobj);
                UIHelper.LoadFocus(txtName);
            }
            if (thaotac == EnumGUI.ThaoTac.xem)
            {
                SetStateButton(thaotac);
                SetTextReadOnly(true);
                loadToUI(Currobj);
            }
        }
        private void SetStateButton(EnumGUI.ThaoTac thaotac)
        {
            switch (thaotac)
            {
                case EnumGUI.ThaoTac.them:
                    btnLuuThem.Visible = true;
                    btnLuuThoat.Visible = true;
                    btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.sua:
                    btnLuuThem.Visible = false;
                    btnLuuThoat.Visible = true;
                    btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.xem:
                    btnLuuThem.Visible = false;
                    btnLuuThoat.Visible = false;
                    btnMacDinh.Visible = false;
                    break;
            }

        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtName.ReadOnly = IsReadOnly;
            txtNote.ReadOnly = IsReadOnly;
        }

        private bool loadToUI(DM_METERIALGROUP obj)
        {
            bool bKQ = false;
            try
            {
                if (obj == null)
                {
                    obj = new DM_METERIALGROUP();
                    DM_METERIALGROUPList list = prov.GetAll();
                    if (list == null) obj.ID = "GM.1";
                    else obj.ID = "GM." + (list.Count + 1).ToString();
                    obj.IDPOS = StaticVar.CPUID;
                }

                labID.Text = obj.ID;
                labPosID.Text = obj.IDPOS;
                txtName.Text = obj.Name;
                txtNote.Text = obj.Note;
                bKQ = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return bKQ;
        }
        private DM_METERIALGROUP loadToValues()
        {
            DM_METERIALGROUP obj = new DM_METERIALGROUP();
            try
            {
                if (labPosID.Text != "0" && txtName.Text != "")
                {
                    obj.ID = labID.Text;
                    obj.IDPOS = labPosID.Text;
                    obj.Name = txtName.Text;
                    obj.Note = txtNote.Text;
                }
                else
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    obj = null;
                }
            }
            catch
            {
                obj = null;
            }
            return obj;
        }
        private bool cmd_XuLy(EnumGUI.ThaoTac thaotac)
        {
            bool KQ = false;
            if (thaotac == EnumGUI.ThaoTac.macdinh) KQ = loadToUI(Currobj);
            else
            {
                DM_METERIALGROUP obj = loadToValues();
                if (obj != null)
                {
                    switch (thaotac)
                    {
                        case EnumGUI.ThaoTac.them:
                            KQ = prov.Cmd_ThemDM_METERIALGROUP(obj);
                            break;
                        case EnumGUI.ThaoTac.sua:
                            KQ = prov.Cmd_CapNhatDM_METERIALGROUP(obj);
                            break;
                    }
                    if (prov.ErrMsg != null)
                        MessageBox.Show(prov.ErrMsg.Replace("DM_METERIALGROUP", "nhóm nvl"), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            return KQ;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            switch (_thaotac)
            { 
                case "luuthoat":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        this.DialogResult = DialogResult.OK;
                        base.Close();
                    }
                    break;
                case "luuthem":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        ControlUI(EnumGUI.ThaoTac.them);
                    }
                    break;
                case "macdinh":
                    KQ = cmd_XuLy(EnumGUI.ThaoTac.macdinh);
                    break;
                case "thoat":
                    this.DialogResult = DialogResult.OK;
                    base.Close();
                    break;
            }
            StaticVar.DanhSachNhomNguyenVatLieu = prov.GetAll();
            UIHelper.LoadFocus(txtName);
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtName.Name)
                    UIHelper.txt_KeyPress(sender, txtNote, e);
                if (txt.Name == txtNote.Name)
                    UIHelper.txt_KeyPress(sender, btnLuuThoat, e);
            }
            catch { }
        }
    }
}
